using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Collections;
namespace LdExpress
{
    public class AgentList
    {
        public long ID = 0;
        public string Country = "";
        public string City = "";
        public string Agent = "";
        public string Address = "";
        public string Mobile = "";
        public string SocialNet = "";
        public string Description = "";
        public DateTime LastUpdate = DateTime.Now;
        public long Sort = 0;
        public int Display = 0;
        public AgentList()
        {
        }

        public AgentList(long lID)
        {
            this.ID = lID;
        }
        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_AgentList";

            if (ID <= 0) // new object, we insert new record to database
            {
                //ID = long.Parse(DBModule.GetNewID(typeof(Bill), "tbl_bill" , "ID", "ID", cn, trans));
                strSQL = "INSERT INTO " + strTableName + "(" +
                "Country, " +
                "City, " +
                "Agent, " +
                "Address, " +
                "Mobile, " +
                "SocialNet," +
                "Description," +
                "Sort, " +
                "Display, " +
                "LastUpdate" +
                ")" +
                "VALUES(" +
                "'" + DBModule.RefineString(Country) + "'," +
                "'" + DBModule.RefineString(City) + "'," +
                "'" + DBModule.RefineString(Agent) + "'," +
                "'" + DBModule.RefineString(Address) + "'," +
                "'" + DBModule.RefineString(Mobile) + "'," +
                "'" + DBModule.RefineString(SocialNet) + "'," +
                "'" + DBModule.RefineString(Description) + "'," +
                Sort + "," +
                Display + "," +
                DBModule.RefineDatetime(LastUpdate) +
                ");";
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                //throw new Exception(strSQL);
                //GV.Response.Write(strSQL);
                UserObjectsLog.SaveObjectActionLog("agentlist", "add", cn, trans);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                 "Country='" + DBModule.RefineString(Country) + "'" +
                 ",City='" + DBModule.RefineString(City) + "'" +
                 ",Agent='" + DBModule.RefineString(Agent) + "'" +
                 ",Address='" + DBModule.RefineString(Address) + "'" +
                 ",Mobile='" + DBModule.RefineString(Mobile) + "'" +
                 ",SocialNet='" + DBModule.RefineString(SocialNet) + "'" +
                 ",Description='" + DBModule.RefineString(Description) + "'" +
                 ",Sort=" + Sort +
                 ",Display=" + Display +
                 ",LastUpdate=" + DBModule.RefineDatetime(LastUpdate) + "" +
                 " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                //GV.Response.Write(strSQL);
                UserObjectsLog.SaveObjectActionLog("agentlist", "edit", cn, trans);
            }
            //throw new Exception(strSQL);
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {

            string strSQL = "DELETE FROM tbl_AgentList WHERE ID = " + this.ID.ToString();

            //GV.Response.Write("sqldelete=" + strSQL);
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("agentlist", "DELETE", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            strSQL = "SELECT * FROM tbl_AgentList WHERE ID = " + ID.ToString() + " limit 0,1";

            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID"))
                    ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Country"))
                    Country = dr["Country"].ToString();
                if (!dr.IsNull("City"))
                    City = dr["City"].ToString();
                if (!dr.IsNull("Agent"))
                    Agent = dr["Agent"].ToString();
                if (!dr.IsNull("Address"))
                    Address = dr["Address"].ToString();
                if (!dr.IsNull("Mobile"))
                    Mobile = dr["Mobile"].ToString();
                if (!dr.IsNull("SocialNet"))
                    SocialNet = dr["SocialNet"].ToString();
                if (!dr.IsNull("Description"))
                    Description = dr["Description"].ToString();
                if (!dr.IsNull("Sort"))
                    Sort = long.Parse(dr["Sort"].ToString());
                if (!dr.IsNull("Display"))
                    Display = int.Parse(dr["Display"].ToString());
                if (!dr.IsNull("LastUpdate"))
                    LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
            }
        }
        public void GetList(string strCondition, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {

            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_AgentList  where 1=1 ";
            if ((strCondition != "") && (strCondition != null))
            {
                strSQL += strCondition;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            else
                strSQL = strSQL + " Order By ID Desc";
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //return strSQL;
        }
        public void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_AgentList where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
    }
}

